<?php
namespace Mapi\Model;
use Think\Model;

class UserModel extends Model {
	protected $_validate = array(
		array('mobile', 'require', '手机号为空', 0, 'regex', 1),
		array('mobile', '/^1[3|4|5|7|8]\d{9}$/', '手机号格式不正确', 0, 'regex', 1),
		array('mobile', 'checkMobileExist', '手机号已被注册', 0, 'callback', 1),
		array('verifycode','require', '验证码不能为空', 0, 'regex', 1),
		array('verifycode','checkTimeVerify', '验证码已过期', 0, 'callback', 1),
		array('verifycode','checkVerify', '验证码不正确', 0, 'callback', 1),
		array('password', 'require', '密码不能为空', 0, 'regex', 1),
		array('repassword', 'checkPwd', '两次密码不一致', 0, 'callback', 1),	
		array('username', 'require', '帐号为空', 1, 'regex', 4),
		// array('username', 'checkLoginAccount', '帐号或手机未注册', 0, 'callback', 4),
		array('password', 'require', '密码为空', 1, 'regex', 4),
		array('password', 'checkLoginPwd', '密码错误', 0, 'callback', 4),
		array('mobile', 'require', '手机号为空', 1, 'regex', 5),
		array('mobile', '/^1[3|4|5|7|8]\d{9}$/', '手机号格式不正确', 1, 'regex', 5),
	);

	protected $_auto = array(
		array('password', 'md5Pwd', 3, 'callback'),
		array('createtime', 'time', 3, 'function'),
		array('lastlogintime', 'time', 3, 'function'),
		array('lastloginip', 'getip', 3, 'callback'),
	);

	/**
	 * 检测短信验证码是否正确
	 * @param  [type] $code [description]
	 * @return [type]       [description]
	 */
	protected function checkVerify() {
		$verifyCode = session('smsCode');
		$postVerifyCode = I('post.verifycode');

		if($verifyCode == $postVerifyCode) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 检测短信验证码是否过期
	 * @param  [type] $code [description]
	 * @return [type]       [description]
	 */
	protected function checkTimeVerify($code) {
		$expireTime = session('smsTime');

		$sTime = 10 * 60;
		if((time() - $expireTime) < $sTime) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 检查发送短信的手机号和提交手机号是否相同
	 * @param  [type] $mobile [description]
	 * @return [type]         [description]
	 */
	protected function checkMobileByBoth() {
		$smsMobile = session('smsMobile');
		$postMobile = I('post.mobile');

		if($smsMobile) {
			if($postMobile != $smsMobile) {
				return false;
			} else {
				return true;
			}		
		} else {
			return true;
		}
	}

	/**
	 * 检测手机是否被注册
	 * @return [type] [description]
	 */
	protected function checkMobileExist() {
		$mobile = I('post.mobile');

		$map['mobile'] = $mobile;
		$res = M('UserInfo')->where($map)->getField('uid');
		if($res) 
			return false;
		else 
			return true;
	}

	protected function checkMobileExist2() {
		$mobile = I('post.mobile');
		$map['mobile'] = $mobile;
		$res = M('UserInfo')->where($map)->getField('uid');
		if($res) 
			return true;
		else 
			return false;
	}

	/**
	 * 加密密码
	 * @param  [type] $password [description]
	 * @return [type]           [description]
	 */
	protected function md5Pwd() {
		$passWord = I('post.password');

		return md5(C('PWD_SALT') . $passWord);
	}

	/**
	 * 检查两次密码是否一致
	 * @return [type] [description]
	 */
	protected function checkPwd() {
		$passWord = I('post.password');
		$rePassWord = I('post.repassword');

		if($passWord != $rePassWord) {
			return false;
		} else {
			return true;
		}
	}

	/**
	 * 检查帐号是否存在
	 * @return [type] [description]
	 */
	protected function checkLoginAccount() {
		$account = I('post.username');
		if(preg_match('/^1[3|4|5|8]\d{9}$/', $account)) {
			$map['mobile'] = $account;
		} else {
			$map['username'] = $account;
		}

		$res = $this->where($map)->getField('uid');
		if($res) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 检查帐号密码是否正确
	 * @return [type] [description]
	 */
	// protected function checkLoginPwd() {
	// 	$account = I('post.username');
	// 	$passWord = I('post.password');

	// 	if(preg_match('/^1[3|4|5|7|8]\d{9}$/', $account)) {
	// 		$map['mobile'] = $account;
	// 	} else {
	// 		$map['username'] = $account;
	// 	}

	// 	$map['password'] = md5(C('PWD_SALT') . $passWord);

	// 	$res = $this->alias('u')->join('__USER_INFO__ AS ui ON ui.uid = u.uid')
	// 				->where($map)
	// 				->find();
	// 	if($res) {
	// 		session('userinfo',$res);
	// 		return true;
	// 	} else {
	// 		return false;
	// 	}

	// }
	
	protected function checkLoginPwd() {
		$account = I('post.username');
		$passWord = I('post.password');

		$map['password'] = md5(C('PWD_SALT') . $passWord);
		$map['mobile'] = $account;
		$res = $this->alias('u')->join('__USER_INFO__ AS ui ON ui.uid = u.uid')
				->where($map)
				->find();	
		if(!$res) {
			$map2['password'] = md5(C('PWD_SALT') . $passWord);
			$map2['realname'] = $account;
			$res2 = $this->alias('u')->join('__USER_INFO__ AS ui ON ui.uid = u.uid')
				->where($map2)
				->find();

			if(!$res2) {
 
				$map3['password'] = md5(C('PWD_SALT') . $passWord);
				$map3['username'] = $account;
				$res3 = $this->alias('u')->join('__USER_INFO__ AS ui ON ui.uid = u.uid')
						->where($map3)
						->find();
				if(!$res3) {
					return false;
				} else {
					session('userinfo',$res3);
					return true;				
				}

			} else {
				session('userinfo',$res2);
				return true;				
			}

		} else {
			session('userinfo',$res);
			return true;			
		}
	}


	protected function getip(){
		return I('server.HTTP_X_REAL_IP');
	}

}
